/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Model;

import Entity.FileTA;
import Entity.User;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import library.Koneksi;


/**
 *
 * @author mr2
 */
public class FileTAModel {
    
        
    Connection koneksi;
    FileTA fileTA;
    User user;
    
    public FileTAModel(){
        koneksi = Koneksi.getKoneksi();
    }
    
    public List<FileTA> ListFile(){
        
        PreparedStatement prepare = null;
        ResultSet result = null;
        List<FileTA> list = new ArrayList<FileTA>();
        
        try{
            String sql = "Select * from fileta";
            
            prepare = koneksi.prepareStatement(sql);
            
            result = prepare.executeQuery();
            
            while(result.next()){
                FileTA fileTA = new FileTA();
                User user = new User();
                //sidangta.setId_user(result.getString("id_user"));
                user.setIdUser(result.getString("id_user"));
                fileTA.setUser(user);
                fileTA.setIdFile(result.getInt("id_file"));
                fileTA.setNamaFile(result.getString("nama_file"));
                fileTA.setLokasiFile(result.getString("lokasi_file"));
                fileTA.setTglFile(result.getString("tanggal_file"));
                list.add(fileTA);
            }
            return list;
            
        }catch(SQLException ex){
            System.out.println(ex.getMessage());
            return list;
        }finally{
            if(prepare != null){
                try{
                    prepare.close();
                }catch(SQLException ex){
                    System.out.println(ex.getMessage());
                }
            }
            if(result != null){
                try{
                    result.close();
                }catch(SQLException ex){
                    System.out.println(ex.getMessage());
                }
            }
        }
    }
    
    public boolean UploadFile(FileTA file){
        
        PreparedStatement prepare = null;
        boolean hasil = true;
        try{
            String sql = "INSERT INTO fileta(id_user, nama_file, lokasi_file, tanggal_file)"
                    + "values(?,?,?,?)";
            
            prepare = koneksi.prepareStatement(sql);
            
            prepare.setString(1, file.getUser().getIdUser());
            prepare.setString(2, file.getNamaFile());
            prepare.setString(3, file.getLokasiFile());
            prepare.setString(4, file.getTglFile());
            
            int status = prepare.executeUpdate();
            if(status > 0){
                hasil = true;
            }else{
                hasil = false;
            }
            
        }catch(SQLException ex){
            hasil = false;
            System.out.println(ex.getMessage());
        }finally{
            if(prepare != null){
                try{
                    prepare.close();
                }catch(SQLException ex){
                    System.out.println(ex.getMessage());
                }
            }
        }
        return hasil;
    }
    
    public boolean DeleteFile(int idFile){
        PreparedStatement prepare = null;
        boolean hasil = true;
        try{
            String sql = "DELETE from fileta where id_file = ? ";
            
            prepare = koneksi.prepareStatement(sql);

            prepare.setInt(1,idFile);
            
            int status = prepare.executeUpdate();
            if(status > 0){
                hasil = true;
            }else{
                hasil = false;
            }
            
        }catch(SQLException ex){
            System.out.println(ex.getMessage());
        }finally{
            if(prepare != null){
                try{
                    prepare.close();
                }catch(SQLException ex){
                    
                }
            }
        }
        return hasil;
    }
}
